{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "====================known data====================\n",
      "1 9\n",
      "3 4\n",
      "1 4\n",
      "0 0\n",
      "2 9\n",
      "2 10\n",
      "2 4\n",
      "1 9\n",
      "0 0\n",
      "0 0\n",
      "===================similar user===================\n",
      "{'film9': 5, 'film12': 5, 'film1': 3, 'film8': 5}\n",
      "=========most similar user and his films==========\n",
      "user1:{'film1': 3, 'film13': 3, 'film2': 4, 'film12': 3, 'film8': 5, 'film3': 5}\n",
      "=================recommended film=================\n",
      "film3\n"
     ]
    }
   ],
   "source": [
    "from random import randrange\n",
    "\n",
    "data={'user'+str(i):{'film'+str(randrange(1,15)):randrange(1,6)\n",
    "                      for j in range(randrange(3,10))}\n",
    "      for i in range(10)}\n",
    "\n",
    "user={'film'+str(randrange(1,15)):randrange(1,6) for i in range(5)}\n",
    "f=lambda item:(-len(item[1].keys()&user),\n",
    "               sum(((item[1].get(film)-user.get(film))**2\n",
    "                    for film in user.keys()&item[1].keys())))\n",
    "similarUser,films=min(data.items(),key=f)\n",
    "\n",
    "print('known data'.center(50,'='))\n",
    "for item in data.items():\n",
    "    print(len(item[1].keys()&user.keys()),\n",
    "          sum(((item[1].get(film)-user.get(film))**2\n",
    "               for film in user.keys()&item[1].keys()))),\n",
    "    item,\n",
    "    sep=':'\n",
    "print('similar user'.center(50,'='))\n",
    "print(user)\n",
    "print('most similar user and his films'.center(50,'='))\n",
    "print(similarUser,films,sep=':')\n",
    "print('recommended film'.center(50,'='))\n",
    "print(max(films.keys()-user.keys(),key=lambda film:films[film]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
